#include "serverinfo.h"

ServerInfo::ServerInfo(QWidget *parent) : QDialog(parent)
{
    ifstream infoFile("config.ini", ios::in);  // on ouvre en lecture
    string IP,PORT,dlPORT,directory;
    if(infoFile)
    {

        getline(infoFile, IP);
        getline(infoFile,PORT);
        getline(infoFile,dlPORT);
        getline(infoFile,directory);
        infoFile.close();


    }
    l_ip = new QLabel(this);
    l_ip->setText("Server IP");
    l_ip->move(25,10);
    l_ip->show();
    ip = new QLineEdit(this);
    ip->move(25,30);
    ip->setText(IP.c_str());
    ip->show();

    l_port = new QLabel(this);
    l_port->setText("Server PORT");
    l_port->move(25,57);
    l_port->show();
    port = new QLineEdit(this);
    port->move(25,80);
    port->setText(PORT.c_str());
    port->show();

    l_dlp = new QLabel(this);
    l_dlp->setText("Download PORT");
    l_dlp->move(25,110);
    l_dlp->show();
    dlp = new QLineEdit(this);
    dlp->move(25,130);
    dlp->setText(dlPORT.c_str());
    dlp->show();

    l_dir = new QLabel(this);
    l_dir->setText("Files Directory");
    l_dir->move(25,160);
    l_dir->show();
    dir = new QLineEdit(this);
    dir->move(25,180);
    dir->setText(directory.c_str());
    dir->show();


    ok = new QPushButton("Save",this);
    ok->move(10,220);
    ok->show();

    cancel = new QPushButton("Cancel",this);
    cancel->move(100,220);
    cancel->show();

    connect(ok,SIGNAL(clicked()),this,SLOT(saveINFO()));
    connect(cancel,SIGNAL(clicked()),this,SLOT(close()));
}
void ServerInfo::saveINFO()
{
    ofstream infoFile("config.ini", ios::out | ios::trunc);
    if(infoFile)
    {
        infoFile<<ip->text().toStdString()<<endl;
        infoFile<<port->text().toStdString()<<endl;
        infoFile<<dlp->text().toStdString()<<endl;
        infoFile<<dir->text().toStdString()<<endl;
        infoFile.close();
    }

    accept();
}
